package com.blog.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.blog.admin.service.MessageService;
import com.blog.common.core.utils.PageUtils;
import com.blog.common.core.web.page.TableDataInfo;
import com.blog.common.domain.entity.FileInfo;
import com.blog.common.domain.entity.Message;
import com.blog.common.domain.dto.MessageDto;
import com.blog.common.domain.vo.MessageVo;
import com.blog.common.mapper.dao.FileInfoMapper;
import com.blog.common.mapper.dao.MessageMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.blog.common.core.constant.CommonConstants.REVIEW_FAIL;
import static com.blog.common.core.constant.CommonConstants.REVIEW_SUCCESS;

/**
 * <p>
 * 留言 服务实现类
 * </p>
 *
 * @author Valerie
 * @since 2023-03-11
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private FileInfoMapper fileInfoMapper;
    @Override
    public TableDataInfo<MessageVo> listMessages(MessageDto messageDto) {
        Page<Message> messagePage = messageMapper.selectMessageList(new Page<>(PageUtils.getPageNum(), PageUtils.getPageSize()), messageDto);
        List<MessageVo> messageVoList = BeanUtil.copyToList(messagePage.getRecords(), MessageVo.class);
        return new TableDataInfo<>(messageVoList, messagePage.getTotal());
    }

    @Override
    public Boolean auditMessage(Serializable id, Boolean review) {
        return new LambdaUpdateChainWrapper<>(messageMapper)
                .eq(Message::getId, id)
                .set(Message::getIsReview, review ? REVIEW_SUCCESS : REVIEW_FAIL)
                .update();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMessage(MessageDto messageDto) {
        FileInfo fileInfo = FileInfo.builder().id(Long.valueOf(messageDto.getAvatar())).build();
        messageDto.setAvatar(fileInfo.getId().toString());
        Message message = BeanUtil.copyProperties(messageDto, Message.class);
        messageMapper.insert(message);
        fileInfoMapper.insert(fileInfo);
    }

    @Override
    public MessageVo getMessage(Serializable id) {
        Message message = this.getById(id);
        return BeanUtil.copyProperties(message, MessageVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMessage(MessageDto messageDto) {
        Message message = BeanUtil.copyProperties(messageDto, Message.class);
        if (StringUtils.isNotBlank(message.getAvatar())){
            FileInfo fileInfo = FileInfo.builder().id(Long.valueOf(message.getAvatar())).build();
            fileInfoMapper.insert(fileInfo);
            message.setAvatar(fileInfo.getId().toString());
        }
        messageMapper.updateById(message);
    }
}
